package com.lxk.controller;

import com.lxk.pojo.Goods;
import com.lxk.pojo.PointGoods;
import com.lxk.pojo.Shop;
import com.lxk.pojo.Ticket;
import com.lxk.service.GoodsService;
import com.lxk.service.PointService;
import com.lxk.service.ShopService;
import com.lxk.service.UtilService;
import com.lxk.vo.GoodsNormalModel;
import com.lxk.vo.GoodsTicketModel;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.CodeMsg;
import com.lxk.vo.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author JohnSoybean
 * @create 2020-03-22 23:54
 * @description 页面跳转控制器
 */
@Controller
public class GoodsController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private GoodsService gs;
    @Autowired
    private ShopService ss;
    @Autowired
    private UtilService us;
    @Autowired
    private PointService ps;

    /**
     * 已下架商品列表展示
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @return
     */
    @GetMapping("/goodsDown")
    public String toGoodsDown(HttpServletRequest req, Model model,
                                Integer pageIndex, Integer num, Integer totalPages,
                                Integer sort, Integer order ) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 10;
        }
        if (null == totalPages) {
            totalPages = gs.getDownGoodsTotalPages(num);
        }
        PageModel<GoodsNormalModel> goodsInfo = gs.selDownGoodsPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", goodsInfo);
        log.info(goodsInfo.toString());
        return "goodsDown";
    }


    /**
     * 普通上架商品列表展示
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @return
     */
    @GetMapping("/goodsNormal")
    public String toGoodsNormal(HttpServletRequest req, Model model,
                                Integer pageIndex, Integer num, Integer totalPages,
                                Integer sort, Integer order ) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 10;
        }
        if (null == totalPages) {
            totalPages = gs.getNormalGoodsTotalPages(num);
        }
        PageModel<GoodsNormalModel> goodsInfo = gs.selNormalGoodsPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", goodsInfo);
        return "goodsNormal";
    }

    /**
     * 卡券类上架商品展示
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/goodsTicket")
    public String toGoodsTicket(HttpServletRequest req, Model model,
                                Integer pageIndex, Integer num, Integer totalPages,
                                Integer sort, Integer order ) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 10;
        }
        if (null == totalPages) {
            totalPages = gs.getTicketGoodsTotalPages(num);
        }
        PageModel<GoodsTicketModel> goodsInfo = gs.selTicketGoodsPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", goodsInfo);
        return "goodsTicket";
    }

    /**
     * 积分类上架商品展示
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/goodsPoint")
    public String toGoodsPoint(HttpServletRequest req, Model model,
                                Integer pageIndex, Integer num, Integer totalPages,
                                Integer sort, Integer order ) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 10;
        }
        if (null == totalPages) {
            totalPages = ps.getPointGoodsTotalPages(num);
        }
        PageModel<PointGoods> goodsInfo = ps.selPointGoodsPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", goodsInfo);
        return "goodsPoint";
    }

    /**
     * 按id查找普通商品信息 附带商家列表
     * @param req
     * @param goodsId
     * @return
     */
    @GetMapping("/goods/query/normal")
    @ResponseBody
    public Result<GoodsNormalModel> queryNormalGoods(HttpServletRequest req, Integer goodsId) {
        GoodsNormalModel model = gs.selNormalGoodsById(goodsId);
        List<Shop> shopList = ss.getShopList();
        model.setShopList(shopList);
        return Result.success(model);
    }

    /**
     * 按id查找卡券商品信息 附带商家列表
     * @param req
     * @param goodsId
     * @return
     */
    @GetMapping("/goods/query/ticket")
    @ResponseBody
    public Result<GoodsTicketModel> queryTicketGoods(HttpServletRequest req, Integer goodsId) {
        GoodsTicketModel model = gs.selTicketGoodsById(goodsId);
        List<Shop> shopList = ss.getShopList();
        model.setShopList(shopList);
        return Result.success(model);
    }

    /**
     * 按id查找积分商品信息
     * @param req
     * @param pointGoodsId
     * @return
     */
    @GetMapping("/goods/query/point")
    @ResponseBody
    public Result<PointGoods> queryPointGoods(HttpServletRequest req, Integer pointGoodsId) {
        PointGoods model = ps.selPointGoodsById(pointGoodsId);
        return Result.success(model);
    }

    /**
     * 修改普通商品
     * @param req
     * @param goods
     * @param file
     * @param lnglat
     * @return
     */
    @PostMapping("/goods/modify/normal")
    @ResponseBody
    public Result<Integer> modifyNormalGoods(HttpServletRequest req,
                                             Goods goods, MultipartFile file, String lnglat) {
        if(!file.isEmpty()){
            String goodsPic = us.uploadPicture(file);
            goods.setGoodsPic(goodsPic);
        }
        if(lnglat!=null){
            Map<String, Double> map = us.substringLnglat(lnglat);
            goods.setGoodsLng(map.get("lng"));
            goods.setGoodsLat(map.get("lat"));
        }
        goods.setModifyTime(new Date());
        gs.modifyGoodsInfo(goods);
        return Result.success(1);
    }


    /**
     * 修改卡券商品
     * @param req
     * @param goods
     * @param file
     * @param lnglat
     * @return
     */
    @PostMapping("/goods/modify/ticket")
    @ResponseBody
    public Result<Integer> modifyTicketGoods(HttpServletRequest req,
                                             Goods goods, String ticketDesc, String ticketRule, Integer ticketTimespan, MultipartFile file, String lnglat) {
        if(!file.isEmpty()){
            String goodsPic = us.uploadPicture(file);
            goods.setGoodsPic(goodsPic);
        }
        if(lnglat!=null){
            Map<String, Double> map = us.substringLnglat(lnglat);
            goods.setGoodsLng(map.get("lng"));
            goods.setGoodsLat(map.get("lat"));
        }
        goods.setModifyTime(new Date());
        gs.modifyGoodsInfo(goods);
        //修改卡券信息
        log.info(ticketDesc);
        Ticket ticket = new Ticket();
        ticket.setTicketDesc(ticketDesc);
        ticket.setTicketRule(ticketRule);
        ticket.setTicketTimespan(ticketTimespan);
        ticket.setGoodsId(goods.getGoodsId());
        ticket.setShopId(goods.getShopId());
        ticket.setTicketName(goods.getGoodsName());
        ticket.setTicketPrice(goods.getGoodsPrice());
        gs.modifyTicket(ticket);
        return Result.success(1);
    }

    /**
     * 修改积分商品
     * @param req
     * @param pointGoods
     * @param file
     * @return
     */
    @PostMapping("/goods/modify/point")
    @ResponseBody
    public Result<Integer> modifyPointGoods(HttpServletRequest req,
                                             PointGoods pointGoods, MultipartFile file) {
        if(!file.isEmpty()){
            String goodsPic = us.uploadPicture(file);
            pointGoods.setPointGoodsPic(goodsPic);
        }
        ps.modifyPointGoodsInfo(pointGoods);
        return Result.success(1);
    }

    /**
     * 切换商品上架状态
     * @param req
     * @param goodsId
     * @param isUpper 0下架 1上架
     * @return
     */
    @GetMapping("/goods/upper/normal")
    @ResponseBody
    public Result<Integer> shiftNormalGoodsUpper(HttpServletRequest req, Integer goodsId, Integer isUpper) {
        Integer upd = gs.shiftGoodsUpper(goodsId, isUpper);
        return Result.success(upd);
    }

    /**
     * 删除商品
     * @param req
     * @param goodsId
     * @return
     */
    @GetMapping("/goods/delete/normal")
    @ResponseBody
    public Result<Integer> deleteNormalGoods(HttpServletRequest req, Integer goodsId) {
        Integer del = gs.deleteGoods(goodsId);
        return Result.success(del);
    }

    /**
     * 删除积分商品
     * @param req
     * @param pointGoodsId
     * @return
     */
    @GetMapping("/goods/delete/point")
    @ResponseBody
    public Result<Integer> deletePointGoods(HttpServletRequest req, Integer pointGoodsId) {
        Integer del = ps.deletePointGoods(pointGoodsId);
        return Result.success(del);
    }

    /*
    返回店铺列表
    goods所有信息
    lnglat信息
    goodsStat生成
    goodsComkey生成
    goodsDetail另外编辑
     */
    /**
     * 添加普通商品
     * @param req
     * @param goods
     * @param file
     * @param lnglat
     * @param isUpper
     * @return
     */
    @PostMapping("/goods/add/normal")
    @ResponseBody
    public Result<Integer> addNormalGoods(HttpServletRequest req,
                                             Goods goods, MultipartFile file, String lnglat, Integer isUpper) {
        if(!file.isEmpty()){
            String goodsPic = us.uploadPicture(file);
            goods.setGoodsPic(goodsPic);
        }else{
            return Result.error(CodeMsg.EMPTY_FILE);
        }
        if(lnglat!=null && !"".equals(lnglat)){
            Map<String, Double> map = us.substringLnglat(lnglat);
            goods.setGoodsLng(map.get("lng"));
            goods.setGoodsLat(map.get("lat"));
        }else{
            Shop shop = ss.selShopById(goods.getShopId());
            if (shop == null) {
                return Result.error(CodeMsg.SHOP_NOT_EXIST);
            }
            goods.setGoodsLng(shop.getShopLng());
            goods.setGoodsLat(shop.getShopLat());
            goods.setGoodsAddr(shop.getShopAddr());
        }
        gs.addNormalGoods(goods, isUpper);
        return Result.success(1);
    }

    /**
     * 添加卡券商品
     * @param req
     * @param goods
     * @param file
     * @param lnglat
     * @param isUpper
     * @return
     */
    @PostMapping("/goods/add/ticket")
    @ResponseBody
    public Result<Integer> addTicketGoods(HttpServletRequest req,
                                          Goods goods, String ticketDesc, String ticketRule, Integer ticketTimespan, MultipartFile file, String lnglat, Integer isUpper) {
        if(!file.isEmpty()){
            String goodsPic = us.uploadPicture(file);
            goods.setGoodsPic(goodsPic);
        }else{
            return Result.error(CodeMsg.EMPTY_FILE);
        }
        if(lnglat!=null && !"".equals(lnglat)){
            Map<String, Double> map = us.substringLnglat(lnglat);
            goods.setGoodsLng(map.get("lng"));
            goods.setGoodsLat(map.get("lat"));
        }else{
            Shop shop = ss.selShopById(goods.getShopId());
            if (shop == null) {
                return Result.error(CodeMsg.SHOP_NOT_EXIST);
            }
            goods.setGoodsLng(shop.getShopLng());
            goods.setGoodsLat(shop.getShopLat());
            goods.setGoodsAddr(shop.getShopAddr());
        }
        gs.addTicketGoods(goods, isUpper);
        //添加卡券
        Ticket ticket = new Ticket();
        ticket.setTicketDesc(ticketDesc);
        ticket.setTicketRule(ticketRule);
        ticket.setTicketTimespan(ticketTimespan);
        ticket.setTicketPrice(goods.getGoodsPrice());
        ticket.setTicketName(goods.getGoodsName());
        ticket.setShopId(goods.getShopId());
        ticket.setGoodsId(goods.getGoodsId());
        gs.addTicket(ticket);
        return Result.success(1);
    }

    /**
     * 添加积分商品
     * @param req
     * @param pointGoods
     * @param file
     * @param isUpper
     * @return
     */
    @PostMapping("/goods/add/point")
    @ResponseBody
    public Result<Integer> addPointGoods(HttpServletRequest req,
                                         PointGoods pointGoods, MultipartFile file, Integer isUpper) {
        if(!file.isEmpty()){
            String goodsPic = us.uploadPicture(file);
            pointGoods.setPointGoodsPic(goodsPic);
        }else{
            return Result.error(CodeMsg.EMPTY_FILE);
        }
        pointGoods.setPointGoodsTime(new Date());
        ps.addPointGoods(pointGoods, isUpper);
        return Result.success(1);
    }


    /**
     * 获取商品详情信息文本
     * @param goodsId
     * @return
     */
    @GetMapping("/goods/text/show")
    @ResponseBody
    public Result<String> showGoodsDetailText(Integer goodsId) {
        String text = gs.getGoodsDetailText(goodsId);
        return Result.success(text);
    }

    /**
     * 更新商品详情文本
     * @param goodsId
     * @param goodsDetail
     * @return
     */
    @PostMapping("/goods/text/modify")
    @ResponseBody
    public Result<Integer> modifyGoodsDetailText(Integer goodsId, String goodsDetail) {
        gs.updGoodsDetailText(goodsId, goodsDetail);
        return Result.success(1);
    }


    /**
     * 获取积分商品详情信息文本
     * @param pointGoodsId
     * @return
     */
    @GetMapping("/point/text/show")
    @ResponseBody
    public Result<String> showPointGoodsDetailText(Integer pointGoodsId) {
        String text = ps.getPointGoodsDetailText(pointGoodsId);
        return Result.success(text);
    }

    /**
     * 更新积分商品详情文本
     * @param pointGoodsId
     * @param pointGoodsDetail
     * @return
     */
    @PostMapping("/point/text/modify")
    @ResponseBody
    public Result<Integer> modifyPointGoodsDetailText(Integer pointGoodsId, String pointGoodsDetail) {
        ps.updPointGoodsDetailText(pointGoodsId, pointGoodsDetail);
        return Result.success(1);
    }

    /**
     * 普通上架商品列表展示(可参与活动的商品)
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @return
     */
    @GetMapping("/goodsActivity")
    public String toGoodsActivity(HttpServletRequest req, Model model,
                                Integer pageIndex, Integer num, Integer totalPages,
                                Integer sort, Integer order ) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 12;
        }
        if (null == totalPages) {
            totalPages = gs.getActivityGoodsTotalPages(num);
        }
        PageModel<GoodsNormalModel> goodsInfo = gs.selActivityGoodsPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", goodsInfo);
        return "goodsActivity";
    }



}
